$first_tped =$ARGV[0];
$second_tped =$ARGV[1];
chomp($first_tped);
chomp($second_tped);
$out_tped = $ARGV[2];
chomp($out_tped);
open(BUFF,$first_tped);
open(WRBUFF,">$out_tped");
while($line=<BUFF>)
{
	chomp($line);
	@array = split(" ",$line);
	$snp = "";
	if($line =~ m/A/)
	{
		$snp = $snp."A";
	}
	if($line =~ m/T/)
        {
                $snp = $snp."T";
        }
	if($line =~ m/G/)
        {
                $snp = $snp."G";
        }
	if($line =~ m/C/)
        {
                $snp = $snp."C";
        }
	$hash{$array[1]} = $snp;
	$hash1{$array[1]} = $array[3];
	$hash5{$array[1]} = $array[0];
}
open(BUFF,$second_tped);
while($line=<BUFF>)
{
        chomp($line);
        @array = split(" ",$line);
	if(exists($hash{$array[1]}))
	{
		
        	$snp = "";
        	if($line =~ m/A/)
        	{
                	$snp = $snp."A";
        	}
        	if($line =~ m/T/)
        	{
                	$snp = $snp."T";
        	}
        	if($line =~ m/G/)
        	{
                	$snp = $snp."G";
        	}
       		if($line =~ m/C/)
        	{
                	$snp = $snp."C";
        	}
		$all_allelle = $hash{$array[1]}.$snp;
		#print "$all_allelle\n";
		$num_totall = 0;
		if($all_allelle =~ m/A/)
                {
			$num_totall++;
		}
		if($all_allelle =~ m/G/)
                {
                        $num_totall++;
                }
		if($all_allelle =~ m/C/)
                {
                        $num_totall++;
                }
		if($all_allelle =~ m/T/)
                {
                        $num_totall++;
                }
		$num_total = 0;
		$temp_snp = $snp;
		$temp_snp =~ tr/ATGC/TACG/;
		$all_allelle =  $hash{$array[1]}.$temp_snp;
		#print "$all_allelle\n";
		if($all_allelle =~ m/A/)
                {
                        $num_total++;
                }
                if($all_allelle =~ m/G/)
                {
                        $num_total++;
                }
                if($all_allelle =~ m/C/)
                {
                        $num_total++;
                }
                if($all_allelle =~ m/T/)
                {
                        $num_total++;
                }
		#print $array[1]."\t".$num_totall."\t".$num_total."\n";	
		if(!($num_totall > 2 && $num_total > 2))
		{
		#	print "success $array[1]\n";
			if($hash{$array[1]} =~ /$snp/ || $snp =~ /$hash{$array[1]}/)
			{
	
			}
			else
			{
				#print $array[1]."\t".$hash{$array[1]}."\t".$snp."\n";
				$line =~ tr/ATGC/TACG/;
				@array = split(" ",$line);
			}
			#if($hash1{$array[1]} ne $array[3])
			#{
				$array[3] = $hash1{$array[1]};
				$array[0] = $hash5{$array[1]};
				$line = join(" ",@array);
			# 	print WRBUFF $line."\n";
			#}
			#else
			#{
				print WRBUFF $line."\n";
			#}
		}
	}	
	else
	{
		print WRBUFF $line."\n";
	}
}
